// packageC/Myshaky/activityHome/activityHome.js
var location = require("../../../mybehaviors/location/location");

var app = getApp();
import util from '../../../utils/util.js';

Page({
  behaviors: [location],

  /**
   * 页面的初始数据
   */
  data: {
    Height: 0,
    LastHeight: 0,
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    imageurl: [],
    categories: [],
    city: '',
    dataList: [],
    nowTime: '',
    len: '',
    konginp: '',
    kFootshow: true,

    set:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 手动更改定位
    if (options.addressData) {
      let addressData = JSON.parse(options.addressData);
      this.setData({
        'point.lat': addressData.latitude,
        'point.lng': addressData.longitude,
        address: addressData.title,
        title: addressData.title,
        city: addressData.city
      });
      console.log(JSON.parse(options.addressData));
    } else {
      console.log('2222222222222222');
      // 获取定位
      this._getLocation(() => {
        this.getlist();
      });
    }
    if (options.message) {
      wx.showToast({
        title: options.message,
        icon: 'none',
        duration: 1000
      });
    }
    this.getlist();
    this.getbanner();
    this.getFl();
  },
  tabrshowbtn(e) {
    let bol = e.detail;
    if (bol) {
      this.setData({
        kFootshow: false
      });
    }
  },
  // 点击分类
  goFenlei(e) {
    let name = e.currentTarget.dataset.name;
    if (name == 'hot' || name == 'poper') {
      var calssify_id = e.currentTarget.id;
      wx.navigateTo({
        url: '/packageC/Myshaky/sort/sort?city=' + this.data.city + "&fenl=" + name + "&id=" + calssify_id,
      });
    } else {
      let calssify_id = e.currentTarget.id;
      // let solt = e.currentTarget.dataset.solt;
      let name = e.currentTarget.dataset.name;
      // let cname = e.currentTarget.dataset.cname;
      wx.navigateTo({
        url: '/packageC/Myshaky/sort/sort?city=' + this.data.city + "&fenl=" + name + "&id=" + calssify_id ,
      });
    }
  },
  tapUrl(e) {
    let link = e.currentTarget.dataset.url;
    if (link == "") return false;
    try {
      wx.navigateTo({
        url: link,
      });
    } catch (err) {
      console.log(err);
    }
  },
  goxq(e) {
    let activity_id = e.currentTarget.id;
    wx.navigateTo({
      url: '/packageC/Myshaky/enrollEnter/enrollEnter?id=' + activity_id,
    });
  },
  goDw(e) {
    wx.navigateTo({
      url: '/packageC/o2o/o2oLocation/o2oLocation?tag=activityHome',
    });
  },
  inpText(e) {
    this.setData({
      inpText: e.detail.value
    });
  },
  goSearch(e) {
    if (this.data.inpText) {
      wx.navigateTo({
        url: '/packageC/Myshaky/searchResult/searchResult?city_name=' + this.data.city + "&keywords=" + this.data.inpText,
      });
    } else {
      wx.navigateTo({
        url: '/packageC/Myshaky/searchResult/searchResult?city_name=' + this.data.city + "&keywords=" + this.data.konginp,
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let json={};
    if(this.data.set){
      if(this.data.set.share_title){
        json.title = this.data.set.share_title;
      }
      if(this.data.set.share_thumb){
        json.imageUrl = this.data.set.share_thumb;
      }
    }
    return json;
  },
  getlist() {
    let urlStr = app.getNetAddresss("plugin.activity-apply.api.index.getActivity");
    let that = this;
    app._getNetWork({
      url: urlStr,
      data: {
        city_name: that.data.city
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          // 推荐
          var recommand = res.data.$recommand;
          var timestamp = Date.parse(new Date());
          var timestamps = timestamp / 1000;
          for (let i = 0; i < recommand.length; i++) {
            recommand[i].start_time = util.formatTimeTwo(recommand[i].start_time, 'Y/M/D h:m:s');
          }
          // 精选的时间处理
          var discount = res.data.discount;
          for (let a = 0; a < discount.length; a++) {
            discount[a].start_time = util.formatTimeTwo(discount[a].start_time, 'M.D');
            discount[a].end_time = util.formatTimeTwo(discount[a].end_time, 'M.D');
          }
          // 热门
          var hot = res.data.hot;
          that.setData({
            dataList: res.data,
            hot: hot,
            discount: discount,
            recommand: recommand,
            nowTime: timestamps,
            len: discount.length,
            set:res.data.set
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  imgHeight: function (e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    var imgh = e.detail.height; //图片高度
    var imgw = e.detail.width; //图片宽度
    var swiperH = winWid * imgh / imgw + "px"; //等比设置swiper的高度。 即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度  ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    if (parseInt(swiperH) > this.data.LastHeight) {
      this.setData({
        Height: swiperH, //设置高度
      });
      this.data.LastHeight = parseInt(swiperH);
    }
  },
  getbanner() {
    let urlStr = app.getNetAddresss("plugin.activity-apply.api.index.getLantetnSlide");
    let that = this;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            imageurl: res.data
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getFl() {
    let urlStr = app.getNetAddresss("plugin.activity-apply.api.index.getCalssify");
    let that = this;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            categories: res.data.reverse()
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }
});